package com.yisu.expressway.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.yisu.expressway.R;
import com.yisu.expressway.shopping_mall.YouZanWebActivity;
import com.yisu.expressway.model.ProductItem;
import com.yisu.expressway.utils.DisplayUtil;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2016/10/5 0005.
 */

public class ProductListAdapter extends  RecyclerView.Adapter<ProductListAdapter.ProductViewHolder>  {

    ArrayList<ProductItem> mData = new ArrayList<>();
    private Context mContext;
    private int ImgSize = 0;
    private int mScreenWidth  = 0;

    public ProductListAdapter (Context context){
        this.mContext = context;
        mScreenWidth = DisplayUtil.getScreenWidth(context);
        ImgSize = (mScreenWidth - DisplayUtil.dip2px(context,28)) / 2 ;
    }

    public void setData(ArrayList<ProductItem> list){
        this.mData = list;
        notifyDataSetChanged();
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View mView = LayoutInflater.from(mContext).inflate(R.layout.product_list_item, parent, false);
        ViewGroup.LayoutParams mLayoutParams = mView.getLayoutParams();
        mLayoutParams.width = ImgSize;
        mView.setLayoutParams(mLayoutParams);
        return new ProductViewHolder(mView);
    }

    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
        if(mData.size() <= position ){
            return;
        }
        final ProductItem item = mData.get(position);
        adjustDefaultViewHeight(holder.mIv_img);
        Glide.with(mContext)
                .load(item.imageUrl)
                .placeholder(R.drawable.blank)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .centerCrop()
                .into(holder.mIv_img);

        holder.mTv_name.setText(item.productName);
        holder.mTv_des.setText(item.productName);
        holder.mTv_price.setText("￥" + item.productPrice);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                YouZanWebActivity.launch(mContext,item.linkUrl,"");
            }
        });

    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    class ProductViewHolder extends RecyclerView.ViewHolder {

        @Bind(R.id.iv_product_img) ImageView mIv_img;
        @Bind(R.id.tv_product_name) TextView mTv_name;
        @Bind(R.id.tv_product_des) TextView mTv_des;
        @Bind(R.id.tv_product_price) TextView mTv_price;

        public ProductViewHolder(View itemView){
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    protected void adjustDefaultViewHeight(View view){
        ViewGroup.LayoutParams mLayoutParams = view.getLayoutParams();
        mLayoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
        mLayoutParams.height = ImgSize;
        view.setLayoutParams(mLayoutParams);
    }
}
